﻿using System;
using System.ComponentModel;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using Microsoft.SharePoint;
using System.Drawing;

namespace OfficeLaunch.SharePoint.TwitterMap.TwitterMapWebPart
{
    [ToolboxItemAttribute(false)]
    public class TwitterMapWebPart : WebPart
    {

        [Personalizable(PersonalizationScope.Shared)]
        [WebBrowsable(true)]
        [Category("Bing Maps")]
        [WebDisplayName("Bing Maps Key")]
        [WebDescription("The API key for the current site to use Bing maps")]
        public string BingMapsKey
        {
            get { return _bingMapsKey; }
            set { _bingMapsKey = value; }
        }

        private string _bingMapsKey;

        private SilverlightPluginGenerator _silverlightPluginGenerator;

        public TwitterMapWebPart()
        {
            _silverlightPluginGenerator = new SilverlightPluginGenerator
            {
                Source = new Uri("/SiteAssets/Silverlight/Twitter Map Web Part/Twitter Map Web Part.xap", UriKind.Relative),
                Width = new Unit(700, UnitType.Pixel),
                Height = new Unit(490, UnitType.Pixel),
                BackGround = Color.White,
                Version = SilverlightVersion.V3,
                AutoUpgrade = true,
                OnError = "onSilverlightError",
            };
        }

        protected override void CreateChildControls()
        {
            base.CreateChildControls();
            Controls.Add(new LiteralControl(@"<script type=""text/javascript"">" + Resources.onSilverlightErrorHandler + "</script>"));
            _silverlightPluginGenerator.InitParams.Add(new InitParam("MS.SP.url", SPContext.Current.Site.Url));
            _silverlightPluginGenerator.InitParams.Add(new InitParam("BingMapsCredential", BingMapsKey));
            Controls.Add(new LiteralControl(_silverlightPluginGenerator.ToString()));
        }
    }
}
